define(['angular',"text!www/trends/trends.html",'url','common','swiper'], function (angular,tpl) {
    //angular会自动根据controller函数的参数名，导入相应的服务
   function controller($scope,$rootScope,$http){
	   initFooter(3);
	   showSpin();
	   bottomMenu();
	   var trends = function () {
	    	 this.banners = [];
	    	 this.top_news = [];
	    	 this.top_acticle = [];
	    	 this.baseUrl = ip+"/";
	    	 this.base = ip;
	    	 this.adUrl = ip+"/"+"data/afficheimg/";
	         this.busy = false;
	         this.wxid = 0;
	     };
	 	$scope.trends = new trends();
	 	
	 	 $scope.trends.initTrends = function(){
	    	 if (this.busy){
	        	 hideSpin();
	        	 return;
	         }
	         this.busy = true;
	         var pro = this;
	         var url = cl.getTrendsUrl+"&callback=JSON_CALLBACK" ;
	         $http.jsonp(url).success(function (res) {
					if(res.code=="CL02023"){//获取成功
						hideSpin();
						pro.top_news = res.result.top_news;
						pro.banners = res.result.ad;
						pro.top_acticle = res.result.top_acticle;
						pro.wxid = res.result.wxid;
					}
					setTimeout("initBox()",1000);
					pro.busy = false;
	         });
	     };
	     $scope.trends.collectNews = function(news_id,index){
	    	 if (this.busy){
	        	 return;
	         }
	    	 if(!this.wxid>0)
	         {
	    		 showAlert("登录后方可收藏");
	    		 return;
	         }
	    	 this.busy = true;
	    	 var pro = this;
	         var url = cl.collectNewUrl+"&wxid="+this.wxid+"&news_id="+news_id+"&callback=JSON_CALLBACK" ;
	         $http.jsonp(url).success(function (res) {
					if(res.code=="CL02026"){//获取成功
						if(res.result.codeMsg=="cancel"){
							showAlert("取消收藏");
							pro.top_news[index].is_collect = " ";
						}else{
							showAlert("已加入收藏");
							pro.top_news[index].is_collect = "collected";
						}
					}else{
						showAlert("收藏失败，请登录后再操作");
					}
					pro.busy = false;
	         });
	     };
	     $scope.trends.likeNew = function(news_id,index){
		    	if(!this.wxid){
		    		showAlert("请登录后方可点赞");
		    		return false;
		    	}
		    	if (this.busy){
			       	 hideSpin();
			       	 return;
			        }
			    this.busy = true;
			    var pro = this;
			    var url = cl.likeNewUrl+"&wxid="+this.wxid +"&news_id="+news_id+"&callback=JSON_CALLBACK" ;
			    $http.jsonp(url).success(function (res) {
					if(res.code=="CL02036"){//获取成功
						if(res.result.codeMsg=="cancel"){
							pro.top_news[index].like_num =  parseInt(pro.top_news[index].like_num) - 1;
							pro.top_news[index].is_like = "";
							showAlert("取消点赞");
						}else{
							pro.top_news[index].like_num = parseInt(pro.top_news[index].like_num) + 1;
							pro.top_news[index].is_like = "liked";
							showAlert("成功点赞");
						}
					}else{
						showAlert("点赞失败，请登录后再操作");
					}
					pro.busy = false;
			    });
		    };
	 	$scope.trends.initTrends();
    };
    return {controller:controller,tpl:tpl};
});
